package cn.ronghuanet.car.controller;

import cn.ronghuanet.car.service.ICarTypeService;
import cn.ronghuanet.car.domain.CarType;
import cn.ronghuanet.car.query.CarTypeQuery;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/carType")
public class CarTypeController {
    @Autowired
    public ICarTypeService carTypeService;


    /**
     * 保存和修改公用的
     * @param carType  传递的实体
     * @return Ajaxresult转换结果
     */
    @PostMapping
    public AjaxResult addOrUpdate(@RequestBody CarType carType){
        if( carType.getId()!=null)
            carTypeService.update(carType);
        else
            carTypeService.insert(carType);
        return AjaxResult.success();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        carTypeService.delete(id);
        return AjaxResult.success();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        CarType carType = carTypeService.selectById(id);
        return AjaxResult.success(carType);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){
        List< CarType> list = carTypeService.selectAll();
        return AjaxResult.success(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PutMapping
    public AjaxResult json(@RequestBody CarTypeQuery query)
    {
        PageInfo<CarType> pageList = carTypeService.queryPage(query);
        return AjaxResult.success(pageList);
    }

    /**
     * 最外面是一级类型
     * 一级类型里面是二级类型
     * 二级类型里面是三级类型
     * ......
     * @return
     */
    @GetMapping("/tree")
    public AjaxResult tree(){
        // 这个地方的list是装的是一级类型
        List<CarType> list = carTypeService.tree(0L);
        return AjaxResult.success(list);
    }
}
